package com.wit.witmg.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.*;

@Configuration
public class StringUtils {

	public static StringUtils instance;
	@PostConstruct
	public void init() {
		instance=this;
	}

	final String webPath="https://apisesame.com/mineshops/upload/";
//	final String comAppid="wx86644f1d6737d4c3";//第三方平台appid
//	final String comAppsecret="83cbba5a4aa085c8df3121739c5c7ba0";//第三方平台secret

	//获取客户端openid
	public String getMyOpenid(HttpServletRequest request){
//		String appId=getAppId(request);
		Object obj=request.getAttribute("openid");
		if(obj==null || obj.toString().equals("")) return "0";
		return obj.toString();
	}

	public JSONObject getBaseResultObj() {
		JSONObject obj=new JSONObject();
		obj.put("code",0);
		obj.put("result",1);
		return obj;
	}

	public Object getResultCountPath(Object val,int count) {
		JSONObject obj=new JSONObject();
		obj.put("code",0);
		obj.put("result",1);
		obj.put("message",val==null?new ArrayList<>():val);
		obj.put("total",count);
		return obj;
	}

	/**
	 * 判断字符串或集合是否为空
	 */
	public boolean isEmpty(Object o) {
		if (o instanceof List)
			return ((List) o).size() == 0;
		return o == null || o.toString().equals("") || o.toString().length()==0;
	}

	public Object getEmptyVal(Object o) {
		return o == null?"":o;
	}

	//班级文件相关路径
	public String getDir(HttpServletRequest request, int type){
		String rootPath = getResourceBasePath();
		String getPath=rootPath+"/upload/";
		if(type==1){
			getPath+="soc/";
		}else if(type==2){
			getPath+="portrait/";
		}else if(type==3){
			getPath+="other/";
		}
		File file=new File(getPath);
		if(!file.exists()){
			file.mkdirs();
		}
		return getPath;
	}


	//微信临时素材库路径
	public String getRegTempDir(){
		String rootPath = getResourceBasePath();
		String getPath=rootPath+"/upload/regTemp/";
		File file=new File(getPath);
		if(!file.exists()){
			file.mkdirs();
		}
		return getPath;
	}


	public String getWebQrPath(){
		return webPath+"qr/";
	}

	public String getWebPosterPath(){
		return webPath+"poster/";
	}

	//获取文件后缀名
	public String getPrefix(String fileName){
		return fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
	}


	////获取班级的服务端访问路径
	public String getWebPath(int type){
		if(type==1){
			return webPath+"soc/";
		}else if(type==2){
			return webPath+"portrait/";
		}else if(type==3){
			return webPath+"other/";
		}
		return webPath;
	}

	public String getWebPath(){
		return webPath;
	}

	/**
	 * @return: int
	 * @description: 十六进制转十进制
	 */
	public int covert(String content){
		int number=0;
		String [] HighLetter = {"A","B","C","D","E","F"};
		Map<String,Integer> map = new HashMap<>();
		for(int i = 0;i <= 9;i++){
			map.put(i+"",i);
		}
		for(int j= 10;j<HighLetter.length+10;j++){
			map.put(HighLetter[j-10],j);
		}
		String[]str = new String[content.length()];
		for(int i = 0; i < str.length; i++){
			str[i] = content.substring(i,i+1);
		}
		for(int i = 0; i < str.length; i++){
			number += map.get(str[i])*Math.pow(16,str.length-1-i);
		}
		return number;
	}


	//图片加水印
	public String getImgUrl(String shopName,String url){
		return url+"!"+(url.contains(".webp")?"/format/jpg":"")+"/watermark/text/"+ Base64Utils.stringToBase64(shopName).replace("/","|")+"/opacity/70/color/ffffff/size/15/align/center/both/600x900";
	}

	public String cutImgUrl(String url){
		return url+"!/watermark/both/200x200";
	}

	public String cutImgUrl300(String url){
		return url+"!/watermark/both/300x300";
	}

	/**
	 * 获取特定时间的 毫秒数
	 */
	public Long getMiol() {
		Calendar ca = Calendar.getInstance();
		//失效的时间
		ca.set(Calendar.HOUR_OF_DAY, 23);
		ca.set(Calendar.MINUTE, 59);
		ca.set(Calendar.SECOND, 59);
		long fl = ca.getTimeInMillis();
		long seconds= (fl - System.currentTimeMillis())/1000; //获取剩余时间
		return seconds;
	}

    public Object getLogoPath() {
		return "https://apisesame.com/mineshops/upload/logo.png";
    }



	public <T> List<T> castList(Object obj, Class<T> clazz)
	{
		List<T> result = new ArrayList<T>();
		if(obj instanceof List<?>)
		{
			for (Object o : (List<?>) obj)
			{
				result.add(clazz.cast(o));
			}
			return result;
		}
		return null;
	}

	/**
	 * 获取小数位数
	 * @param value
	 * @return
	 */
	public Integer getNumOfPlace(Double value) {
		final BigDecimal bigDecimal = new BigDecimal("" + value);
		final String str = bigDecimal.toPlainString();
		final int index = str.indexOf('.');
		if (index < 0) {
			return 0;
		}
		return str.length() - 1 - index;
	}

	/**
	 * 获取项目根路径
	 * @return
	 */
	public String getResourceBasePath() {
		// 获取跟目录
		File path = null;
		try {
			path = new File(ResourceUtils.getURL("classpath:").getPath());
		} catch (FileNotFoundException e) {
		// nothing to do
		}
		if (path == null || !path.exists()) {
			path = new File("");
		}

		String pathStr = path.getAbsolutePath();
		// 如果是在eclipse中运行，则和target同级目录,如果是jar部署到服务器，则默认和jar包同级
		pathStr = pathStr.replace("\\target\\classes", "");
		pathStr = pathStr.replace("\\WEB-INF\\classes", "");
		pathStr = pathStr.replace("/WEB-INF/classes", "");

		return pathStr;
	}

	//字符串加密
	public String secret (String value){
		char secret='8'; //加密文字符
		byte[] bt=value.getBytes(); //将需要加密的内容转换为字节数组
		for (int i = 0; i < bt.length; i++) {
			bt[i]=(byte)(bt[i]^(int)secret); //通过异或运算进行加密
		}
		return new String(bt,0,bt.length);
	}

	//字符串处理
    public String regStr(String json) {
		json=json.replaceAll("\n","");
		json=json.replaceAll("\t","");
		json=json.replaceAll(" ","");
		return json;
    }

    //员工手机号处理
    public String getStrPhone(String phone){
		phone=phone.replaceAll(" ","");
		phone=phone.replaceAll("-","");
		phone=phone.replaceAll("\\*","");
		phone=phone.replaceAll("#","");
		phone=phone.replaceAll("\\+86","");
		return phone;
	}

	public Long getGuid(HttpServletRequest request) {
		Object obj=request.getAttribute("guid");
		if(obj==null || obj.toString().equals("")) return 0L;
		return Long.parseLong(obj.toString());
	}

	//日志文件路径
	public String getLogDir(){
		String rootPath = getResourceBasePath();
		String getPath=rootPath+"/upload/log/";
//		String getPath=System.getProperty("user.dir") + "/upload/qr/";
		File file=new File(getPath);
		if(!file.exists()){
			file.mkdirs();
		}
		return getPath;
	}
}
